import streamlit as st
import logging
from datetime import datetime

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler('app.log', encoding='utf-8'),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)
if "role" not in st.session_state:
    st.session_state.role = None


def login():
    st.header("登录")
    role = st.selectbox("选择角色", [None, "实验管理", "系统管理"])
    if st.button("确定"):
        st.session_state.role = role
        st.rerun()


def logout():
    st.session_state.role = None
    st.rerun()


input_role = st.session_state.role

logout_page = st.Page(logout, title="退出", icon=":material/logout:")
settings = st.Page("settings.py", title="设置", icon=":material/settings:")

data_page1 = st.Page("business/pages/data1.py", title="数据可视化", icon=":material/ac_unit:")
data_page2 = st.Page("business/pages/data2.py", title="异常值检测", icon=":material/add_task:")

request_1 = st.Page(
    "business/pages/home.py",
    title="主页",
    icon=":material/house:",
    default=True,
)
request_2 = st.Page(
    "business/pages/request_1.py", title="膜污染预测", icon=":material/ads_click:"
)
request_3 = st.Page(
    "business/pages/request_2.py", title="反洗智能优化", icon=":material/art_track:"
)
request_4 = st.Page(
    "business/pages/request_3.py", title="故障诊断", icon=":material/baby_changing_station:"
)
admin_1 = st.Page(
    "business/pages/admin/admin_1.py",
    title="用户管理",
    icon=":material/person_add:"
)
admin_2 = st.Page("business/pages/admin/admin_2.py", title="算法配置", icon=":material/security:")

account_pages = [logout_page, settings]
request_pages = [request_1, request_2, request_3, request_4]
data_pages = [data_page1, data_page2]
admin_pages = [admin_1, admin_2]

# st.title("智能直供水运维平台")
st.logo("business/images/horizontal_blue.png", icon_image="business/images/icon_blue.png")

page_dict = {}
logger.info(page_dict)
if st.session_state.role in ["实验管理", "系统管理"]:
    page_dict["实验管理"] = request_pages
    page_dict["数据管理"] = data_pages
if st.session_state.role == "系统管理":
    page_dict["系统管理"] = admin_pages

if len(page_dict) > 0:
    pg = st.navigation(page_dict | {"账户管理": account_pages})
else:
    pg = st.navigation([st.Page(login)])
logger.info("------------------")
logger.info(page_dict)
pg.run()
